package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.day3.CategoryRespVo;
import com.cskaoyan.bean.day3.Day3Data;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stone
 * @date 2022/09/08 15:11
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    //Service → Dao（Mapper）
    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public CategoryRespVo list() {
        // 写业务，通过Mapper去查询，去更新、去修改、去删除 → 做对应的业务 → 响应CategoryRespVo
        // 先查询一级类目 → 根据level = “L1”查询出一级类目
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();//条件列表List<Criterion>
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> l1Categories = categoryMapper.selectByExample(example);

        // 放入pageInfo中的应该Mapper查询出来的结果
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(l1Categories);

        // 然后在查询二级类目
        for (MarketCategory l1Category : l1Categories) {
            Integer id = l1Category.getId();
            MarketCategoryExample innerExample = new MarketCategoryExample();
            innerExample.createCriteria().andPidEqualTo(id).andLevelEqualTo("L2");
            List<MarketCategory> l2Category = categoryMapper.selectByExample(innerExample);
            l1Category.setChildren(l2Category);
        }
        //PageInfo<MarketCategory> pageInfo = new PageInfo<>(l1Categories);
        // page → pageInfo.get
        // limit → pageInfo.get
        // pages → pageInfo.get
        // total → pageInfo.get
        // list → l1Categories
        int total = (int)pageInfo.getTotal();
        int page = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        Day3Data day3Data = new Day3Data(limit, page, pages, total, l1Categories);
        CategoryRespVo respVo = new CategoryRespVo(day3Data, "成功", 0);

        return respVo;
    }

    @Override
    public void modify(MarketCategory marketCategory) {
        int i = categoryMapper.updateByPrimaryKey(marketCategory);
    }
}
